﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using eTerm.MsSql.DbProvider;

namespace eTerm.Platform.Web.Order
{
    public partial class Assign : BasePage
    {
        /// <summary>
        /// 页面初始化.
        /// </summary>
        protected override void InitPageLoad()
        {
            dlCompany.DataSource = new List<SysCompany>() { new SysCompany() { CompayName = "全部", CompanyCode = string.Empty } }.Union(PnrCabin.All().Select(l => l.AirWayCode).Distinct().ToList().Select(l=>new SysCompany(){ CompanyCode=l, CompayName=l }));
            dlCompany.DataBind();
            long ID = long.Parse(Request["ID"]);
            EntityIdentity = ID;
            QueryData();
        }

        protected void btnQuery_Click(object sender, EventArgs e) {
            AspNetPager1.CurrentPageIndex = 1;
            QueryData();
        }

        public long EntityIdentity
        {
            get
            {
                if (ViewState["EntityKey"] == null)
                    return 0;
                else
                    return (long)ViewState["EntityKey"];
            }
            set
            {
                ViewState["EntityKey"] = value;
            }
        }



        /// <summary>
        /// 初始化EasyUI控件CSS
        /// </summary>
        protected override void InitEasyUI()
        {
            BaseRegisterStartupScript<UpdatePanel>(GetUpdatePanel(@"pnlContent") as UpdatePanel, btnQuery.ClientID, string.Format(@"$('#{0}').linkbutton({{   
    iconCls: 'icon-search'  
}});  
", btnQuery.ClientID), true);

            BaseRegisterStartupScript<UpdatePanel>(GetUpdatePanel(@"pnlContent") as UpdatePanel, dlCompany.ClientID, string.Format(@"$('#{0}').combobox();  
", dlCompany.ClientID), true);

        }


        private void QueryData() {
            var sql = PnrCabin.All()//.Where(l=>(string.IsNullOrEmpty(s.FlightNo) ? true : s.FlightNo.Substring(0, 2) == r.AirWayCode))
                //.SelectMany(c => PnrRequireCode.All().Where(rc => rc.SegmentId == EntityIdentity && rc.CabinId == c.Id).DefaultIfEmpty(), (l, r) => new { l.AirWayCode, l.CabinCode, l.Description, l.Discount, l.Id, AssignId = r.Id })
                //.SelectMany(r => PnrSegment.All().Where(p => p.Id == EntityIdentity && (string.IsNullOrEmpty(p.FlightNo) ? 1 != 1 : (p.FlightNo.Substring(0, 2) == r.AirWayCode && p.CabinCode == r.CabinCode))).DefaultIfEmpty(), (l, s) => new { l.AirWayCode, l.CabinCode, l.Description, l.Discount, l.Id, l.AssignId, PrimaryId = s.Id })
                //.Select(l => new {l.AirWayCode,l.CabinCode,l.Description,l.Discount,l.Id,Selected = (l.AssignId!=0 || l.PrimaryId!=0) });
                ;

            //if (!string.IsNullOrEmpty(dlCompany.SelectedValue))
            //    sql = sql.Where(l => l.AirWayCode == dlCompany.SelectedValue);


            //sql = sql.OrderBy(l => l.Discount).OrderByDescending(l => l.AirWayCode);


            //AspNetPager1.RecordCount = sql.Count();
            //rptSegment.DataSource = sql.Skip((AspNetPager1.CurrentPageIndex - 1) * AspNetPager1.PageSize).Take(AspNetPager1.PageSize).ToList();

            this.AspNetPager1.CustomInfoHTML = string.Format("当前第{0}/{1}页 共{2}条记录 每页{3}条", new object[] { this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageCount, this.AspNetPager1.RecordCount, this.AspNetPager1.PageSize });

            rptSegment.DataBind();
        }

        protected void rptSegment_OnItemDataBound(object sender, RepeaterItemEventArgs e) {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
                var model = e.Item.DataItem;
                bool Selected = ((bool)Get<object>(model, @"Selected"));
                LinkButton btnSelect = e.Item.FindControl(@"btnSelect") as LinkButton;
                int ID = (int)Get<object>(model, "Id");
                if (Selected) {
                    btnSelect.Text = @"删除";
                    BaseRegisterStartupScript<UpdatePanel>(GetUpdatePanel(@"pnlContent") as UpdatePanel, btnSelect.ClientID, string.Format(@"$('#{0}').linkbutton({{   
    iconCls: 'icon-delete'  
}});  
", btnSelect.ClientID), true);
                    btnSelect.OnClientClick = string.Format(@"javascript:$.jBox.confirm(""确实要删除该级别舱位？"", ""删除提示"", function (v, h, f){{ if(v){{ $.post('../ISAPI/SysUserAPI.ashx',{{'Action':'DeleteCabin','RequireID':'{0}','CabinID':'{1}'  }},function(){{ $.jBox.tip('舱位已删除。','info');$('#{2}').remove(); }}); }} return true; }}, {{ buttons: {{ '确认': true, '取消': false}} }});return false;", EntityIdentity, ID, btnSelect.ClientID);
                }
                else
                {
                    btnSelect.Text = @"选择";
                    BaseRegisterStartupScript<UpdatePanel>(GetUpdatePanel(@"pnlContent") as UpdatePanel, btnSelect.ClientID, string.Format(@"$('#{0}').linkbutton({{   
    iconCls: 'icon-sum'  
}});  
", btnSelect.ClientID), true);
                    btnSelect.OnClientClick = string.Format(@"javascript:$.jBox.confirm(""确实选择该级别舱位？"", ""选择提示"", function (v, h, f){{ if(v){{ $.post('../ISAPI/SysUserAPI.ashx',{{'Action':'SelectCabin','RequireID':'{0}','CabinID':'{1}'  }},function(){{ $.jBox.tip('舱位已选择。','info');$('#{2}').remove(); }}); }} return true; }}, {{ buttons: {{ '确认': true, '取消': false}} }});return false;", EntityIdentity, ID, btnSelect.ClientID);
                }

                //btnSelect.Visible = !;
            }
        }

        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            QueryData();
        }


    }
}
